package com.ala4.oxcafe.domain;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 响应信息主体
 */
@Data
@Schema(description = "响应信息主体")
public class R<T> implements Serializable {

    @Schema(description = "响应时间")
    private LocalDateTime timestamp;

    @Schema(description = "响应状态")
    private int status;

    @Schema(description = "响应消息")
    private String message;

    @Schema(description = "错误信息")
    private String error;

    @Schema(description = "响应数据")
    private T data;

    @Schema(description = "请求路径")
    private String path;

    public R() {
        this.timestamp = LocalDateTime.now();
        this.status = HttpStatus.OK.value();
        this.message = HttpStatus.OK.getReasonPhrase();
    }

    public R(T data) {
        this();
        this.data = data;
    }

    public R(String message) {
        this();
        this.message = message;
    }

    public R(HttpStatus status, String message, String path, String error) {
        this();
        this.status = status.value();
        this.message = message;
        this.path = path;
        this.error = error;
    }

    public static R<?> fail(String message, String path, String error) {
        return new R<>(HttpStatus.INTERNAL_SERVER_ERROR, message, path, error);
    }

    public static R<?> fail(String message, String path) {
        return new R<>(HttpStatus.INTERNAL_SERVER_ERROR, message, path, message);
    }

    public static <T> R<T> ok(T data) {
        return new R<>(data);
    }

    public static <T extends Serializable> R<T> ok() {
        return new R<>();
    }

    public static <T> R<T> ok(String message) {
        return new R<>(message);
    }
}
